package com.yhsoft.bill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhsoft.bill.controller.request.SysYckLogInVo;
import com.yhsoft.bill.controller.request.VenderYszkMxInVo;
import com.yhsoft.bill.controller.response.SysCzMxOutVo;
import com.yhsoft.bill.controller.response.SysXhLogOutVo;
import com.yhsoft.bill.entity.VenderYszkMx;
import com.yhsoft.bill.mapper.VenderYszkMxMapper;
import com.yhsoft.bill.service.IVenderYszkMxService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.buyer.controller.response.VenderOrdersOutVo;
import com.yhsoft.common.service.TokenService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * <p>
 * 供货商应收账款 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-11-03
 */
@Service
public class VenderYszkMxServiceImpl extends ServiceImpl<VenderYszkMxMapper, VenderYszkMx> implements IVenderYszkMxService {

    @Override
    public IPage<SysCzMxOutVo> getYsMx(VenderYszkMxInVo inVo, Long pageNo, Long pageSize) {
        QueryWrapper<SysCzMxOutVo> wrapper = new QueryWrapper<>();
        wrapper.eq("a.company_id", TokenService.getUserBytoken().getCompanyId());

        // 查询条件
        wrapper.ge((inVo.getStartTime() != null), "a.pay_time", inVo.getStartTime());
        if (inVo.getEndTime() != null) {
            wrapper.le( "a.pay_time", LocalDateTime.of(inVo.getEndTime(), LocalTime.MAX));
        }

        wrapper.orderByDesc("a.create_time");

        Page<SysCzMxOutVo> page = new Page<>(pageNo, pageSize);
        IPage<SysCzMxOutVo> iPage = baseMapper.getYsMxList(page, wrapper);

        return iPage;
    }

    @Override
    public IPage<SysXhLogOutVo> getXhLog(SysYckLogInVo inVo, Long pageNo, Long pageSize) {
        QueryWrapper<VenderOrdersOutVo> wrapper = new QueryWrapper<>();
        wrapper.eq("d.vendor_id", TokenService.getUserBytoken().getCompanyId());
        wrapper.eq("a.order_status", 0);
        // 查询条件
        wrapper.like(StringUtils.hasLength(inVo.getOrderNo()), "a.order_id", inVo.getOrderNo());
        wrapper.like(StringUtils.hasLength(inVo.getVenderOrderNo()), "a.order_id", inVo.getVenderOrderNo());
        wrapper.like(StringUtils.hasLength(inVo.getGoodsName()), "d.goods_name", inVo.getGoodsName());
        wrapper.ge((inVo.getStartTime() != null), "a.pay_time", inVo.getStartTime());
        if (inVo.getEndTime() != null) {
            wrapper.le("a.pay_time", LocalDateTime.of(inVo.getEndTime(), LocalTime.MAX));
        }

        Page<SysXhLogOutVo> page = new Page<>(pageNo, pageSize);
        IPage<SysXhLogOutVo> iPage = this.baseMapper.getListPageByConditions(page, wrapper);

        return iPage;
    }
}
